//
//  WJMainTabBarItem.swift
//  PDFSign
//
//  Created by 李永彬 on 2025/9/18.
//

import UIKit

class WJMainTabBarItem: UIControl {

    var title: String? {
        didSet {
            updateLayout()
        }
    }
    
    var icon: UIImage? {
        didSet {
            iconImageView.image = icon
        }
    }
    
    var selectedIcon: UIImage?
    
    var normalColor: UIColor = .gray {
        didSet {
            updateAppearance()
        }
    }
    
    var selectedColor: UIColor = .systemBlue {
        didSet {
            updateAppearance()
        }
    }
    
    override var isSelected: Bool {
        didSet {
            updateAppearance()
        }
    }
    
    private let iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 10)
        label.textAlignment = .center
        return label
    }()
    
    private let stackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .center
        stack.distribution = .fill
        stack.spacing = 4
        stack.isUserInteractionEnabled = false
        return stack
    }()
    
    private var iconWidthConstraint: NSLayoutConstraint?
    private var iconHeightConstraint: NSLayoutConstraint?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupView()
    }
    
    private func setupView() {
        addSubview(stackView)
        
        stackView.addArrangedSubview(iconImageView)
        stackView.addArrangedSubview(titleLabel)
        
        stackView.translatesAutoresizingMaskIntoConstraints = false
        
        // 创建约束
        iconWidthConstraint = iconImageView.widthAnchor.constraint(equalToConstant: 24)
        iconHeightConstraint = iconImageView.heightAnchor.constraint(equalToConstant: 24)
        
        NSLayoutConstraint.activate([
            stackView.centerXAnchor.constraint(equalTo: centerXAnchor),
            stackView.centerYAnchor.constraint(equalTo: centerYAnchor),
            iconWidthConstraint!,
            iconHeightConstraint!
        ])
        
        // 初始布局更新
        updateLayout()
    }
    
    private func updateLayout() {
        let isSpecialButton = (title?.isEmpty == true)
        
        if isSpecialButton {
            titleLabel.isHidden = true
            iconWidthConstraint?.constant = 48
            iconHeightConstraint?.constant = 48
        } else {
            titleLabel.isHidden = false
            titleLabel.text = title
            iconWidthConstraint?.constant = 24
            iconHeightConstraint?.constant = 24
        }
        
        // 添加动画效果
        UIView.animate(withDuration: 0.2) {
            self.layoutIfNeeded()
        }
    }
    
    private func updateAppearance() {
        if isSelected {
            titleLabel.textColor = selectedColor
            iconImageView.image = selectedIcon ?? icon
            iconImageView.tintColor = selectedColor
        } else {
            titleLabel.textColor = normalColor
            iconImageView.image = icon
            iconImageView.tintColor = normalColor
        }
    }
}
